from sklearn.model_selection import KFold           #k-交叉验证
from sklearn.model_selection import cross_val_score   #交叉验证
from sklearn.ensemble import BaggingClassifier        #装袋算法
from sklearn.tree import DecisionTreeClassifier       #决策树分类
from sklearn import datasets                          #数据集

iris = datasets.load_iris()
X = iris.data
Y = iris.target

kfold = KFold(n_splits=10, shuffle=True, random_state=42)  #random_state 表示是否固定随机起点，Used when shuffle == True.
cart = DecisionTreeClassifier(max_depth=2)       #criterion='gini' 默认
cart = cart.fit(X, Y)
result = cross_val_score(cart, X, Y, cv=kfold)   #cv：交叉验证生成器或可迭代的次数
print("CART树结果： ",result.mean())

model=BaggingClassifier(base_estimator=cart,n_estimators=100, random_state=42)
result = cross_val_score(model, X, Y, cv=kfold)
print("装袋法提升后结果： ",result.mean())


